import { onHide } from '@dcloudio/uni-app';
<!--
 * @Description:
 * @Date: 2024-05-14 09:00:16
 * @LastEditTime: 2024-08-26 14:57:19
-->
<route lang="json5" type="mine">
{
  style: {
    navigationBarTitleText: '重置密码',
    navigationBarTextStyle: 'white', //标题栏字体颜色
    navigationBarBackgroundColor: '#0063AF', //标题栏背景色(纯色)
  },
}
</route>
<template>
  <view class="w-screen h-screen flex flex-col p-20rpx box-border bg-#fff">
    <div>
      <!-- <view class="footer">
        <wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
      </view> -->
      <wd-form ref="form" :model="state.pwd">
        <wd-input
          label="旧密码"
          prop="old"
          show-password
          clearable
          :maxlength="20"
          v-model="state.pwd.old"
          placeholder="请输入旧密码"
          :rules="[{ required: true, message: '请填写旧密码' }]"
        />
        <wd-input
          label="新密码"
          prop="new"
          show-password
          clearable
          :maxlength="20"
          v-model="state.pwd.new"
          placeholder="请输入新密码"
          :rules="[{ required: true, message: '请填写新密码' }]"
        />
        <wd-input
          label="确认密码"
          prop="confirmPwd"
          show-password
          :maxlength="20"
          clearable
          v-model="state.pwd.confirmPwd"
          placeholder="请输入确认密码"
          :rules="[{ required: true, message: '请填写确认密码' }]"
        />
        <view class="footer">
          <wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
        </view>
      </wd-form>
    </div>
  </view>
</template>

<script setup lang="ts">
import { useUserStore } from '@/store'
import { useToast } from 'wot-design-uni'
import { updateUserPassword, logout } from '@/service/index/foo'

onShow(() => {
  currentRole.value = userStore.userInfo.currentRole
})

const toast = useToast()
const baseurl = import.meta.env.VITE_SERVER_BASEURL
const userStore = useUserStore()
const currentRole = ref('')
const filesList = ref([])
const form = ref()

const state = reactive({
  basicInfo: {},
  fileObj: {},
  pwd: {
    old: '',
    new: '',
    confirmPwd: '',
  },
})

function handleSubmit() {
  form.value
    .validate()
    .then(({ valid, errors }) => {
      if (valid) {
        if (state.pwd.confirmPwd !== state.pwd.new) {
          return toast.error({
            msg: '新密码和确认密码不一致，请修改！',
          })
        }
        const data = {
          oldPassword: state.pwd.old,
          newPassword: state.pwd.new,
        }
        updateUserPassword(data).then(async (res) => {
          if (res.data) {
            toast.success({
              msg: '密码重置成功！',
            })
            await logout().then((res) => {
              if (res.data) {
                nextTick(() => {
                  uni.reLaunch({ url: '/pages/home/index' })
                  userStore.clearUserInfo()
                })
              }
            })
            return
          }
          toast.error({
            msg: res.msg,
          })
        })
      }
    })
    .catch((error) => {
      console.log(error, 'error')
    })
}
</script>
<style lang="scss" scoped>
.info {
  display: flex;
  padding: 0px 16px;

  &-basic {
    display: flex;
    flex-direction: column;
  }

  &-tag {
    height: 18px;
    padding: 4px 16px;
    // display: flex;
    // align-items: center;
    // height: 22px;
    // padding: 4px 16px;
    margin-left: auto;
    font-size: 14px;
    font-weight: 400;
    border-radius: 20px;
  }
}

.divider {
  padding: 4px 16px;
  margin: 8px 0px;
  font-weight: bold;
  background-color: #e9f3ff;
}

.wd-textarea {
  background-color: #f9f9f9;
}

::v-deep .wd-textarea__inner {
  background-color: #f9f9f9;
}
</style>
